package com.xieyong.mywebsite.controller;

import com.github.pagehelper.PageInfo;
import com.xieyong.mywebsite.model.dto.Result;
import com.xieyong.mywebsite.model.entity.BaseDict;
import com.xieyong.mywebsite.service.BaseDictService;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/dict")
public class BaseDictController {

    @Resource
    private BaseDictService baseDictService;

    @GetMapping("/id/{id}")
    public Result getDictById(@PathVariable Integer id) {
        BaseDict BaseDict = baseDictService.selectOne(id);
        return Result.success(BaseDict);
    }

    @GetMapping("/{dictid}")
    public Result getDictByDictId(@PathVariable String dictid) {
        BaseDict BaseDict = baseDictService.selectOneByDictId(dictid);
        return Result.success(BaseDict);
    }


    @GetMapping("/name/{name}")
    public Result getDictByDictName(@PathVariable String name) {
        BaseDict BaseDict = baseDictService.selectOneByDictName(name);
        return Result.success(BaseDict);
    }

    @GetMapping()
    public Result getAll() {
        List<BaseDict> dicts = baseDictService.selectAll();
        return Result.success(dicts);
    }

    @GetMapping("/page")
    public Result getPage(@RequestParam Integer pageNum,
                          @RequestParam Integer pageSize,
                          @RequestParam(required = false) String topId,
                          @RequestParam(required = false) String preId,
                          @RequestParam(required = false) String name) {
        PageInfo<BaseDict> pageInfo = baseDictService.selectPage(pageNum, pageSize, topId, preId, name);
        return Result.success(pageInfo);
    }

    @GetMapping("/topid")
    public Result getDictByTopid(@RequestParam String topId) {
        List<BaseDict> BaseDicts = baseDictService.selectDictsByTopId(topId);
        return Result.success(BaseDicts);
    }

    @GetMapping("/preid")
    public Result getDictByPreId(@RequestParam String preId) {
        List<BaseDict> BaseDicts = baseDictService.selectDictsByPreID(preId);
        return Result.success(BaseDicts);
    }

    @PostMapping()
    public Result add(@RequestBody BaseDict BaseDict) {
        baseDictService.insert(BaseDict);
        return Result.success();
    }

    @PutMapping()
    public Result update(@RequestBody BaseDict BaseDict) {
        baseDictService.update(BaseDict);
        return Result.success();
    }

    @PostMapping("/switchactive/{id}")
    public Result switchActive(@PathVariable Integer id) {
        baseDictService.switchActive(id);
        return Result.success();
    }

    @PostMapping("/switchactive/batch")
    public Result switchActive(@RequestBody List<Integer> ids) {
        baseDictService.switchBatchActive(ids);
        return Result.success();
    }

    @DeleteMapping("/{id}")
    public Result delete(@PathVariable Integer id) {
        baseDictService.delete(id);
        return Result.success();
    }

    @DeleteMapping("/batch")
    public Result deleteBatch(@RequestBody List<Integer> ids) {
        baseDictService.deleteBatch(ids);
        return Result.success();
    }
}
